package com.huluobo.day11_im.register.view;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.huluobo.day11_im.R;
import com.huluobo.day11_im.register.presenter.RegisterContract;
import com.huluobo.day11_im.register.presenter.RegisterPresenter;
import com.hyphenate.chat.EMClient;

public class RegisterActivity extends AppCompatActivity implements RegisterContract.View {
    private EditText username, password, rePassword, phonetext;

    private RegisterPresenter registerPresenter;

    private ProgressDialog progressDialog;
    private Button timeButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        username = findViewById(R.id.register_username);
        password = findViewById(R.id.register_password);
        rePassword = findViewById(R.id.register_rePassword);

        progressDialog = new ProgressDialog(this);

        registerPresenter = new RegisterPresenter(this);

        timeButton = (Button) findViewById(R.id.timebutton);
        //new倒计时对象,总共的时间,每隔多少秒更新一次时间
        CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //防止计时过程中重复点击
                timeButton.setEnabled(false);
                timeButton.setText(millisUntilFinished / 1000 + "秒");
            }

            @Override
            public void onFinish() {
                //重新给Button设置文字
                timeButton.setText("重新获取");
                //设置可点击
                timeButton.setEnabled(true);
            }
        };

        //设置Button点击事件触发倒计时
        timeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                countDownTimer.start();
            }
        });
    }


    public void register(View view) {
        String usernameString = username.getText().toString();
        String passwordString = password.getText().toString();
        String rePasswordString = rePassword.getText().toString();
        registerPresenter.register(usernameString, passwordString, rePasswordString);
    }

    @Override
    public void onRegisterSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog.dismiss();
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    @Override
    public void onRegisterFailed(String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog.dismiss();
                Toast.makeText(RegisterActivity.this, "注册失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onStartRegister() {
        progressDialog.setMessage("正在注册...");
        progressDialog.show();
    }

    @Override
    public void onUsernameError() {
        username.setError(getResources().getString(R.string.login_username_text));
    }

    @Override
    public void onPasswordError() {
        password.setError(getResources().getString(R.string.login_password_text));
    }

    @Override
    public void onRePasswordError() {
        rePassword.setError(getResources().getString(R.string.rePasswordError));
    }

}

